# -*- coding: utf-8 -*-
#apply_thread_update.py
#register thread update requests and return OK if accepted

import wsgiref.handlers
from google.appengine.ext import webapp
import sys, time, uuid
import os
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from xmw import tianya
from xmw.tianya.bot import *
import xmw
from xmw.tianya import error_tianya

class MainPage(xmw.RequestHandler):
  def get(self):
    if  not (self.request.GET.has_key('url') and \
             self.request.GET.has_key('latest') ):    #self.request.GET.has_key('thread_id') ):
        #e = error_tianya.ARGUMENT_FAULT("URI:/scan_thread_update?url=XXX&latest=YY-MM-DD HH:MM:SS&thread_id=XXXXXX")
        e = error_tianya.ARGUMENT_FAULT("URI:/apply_thread_update?url=XXX&latest=YY-MM-DD HH:MM:SS")
        self.render_error(e)
        return
    
    #argument right
    url = self.request.GET['url']
    latest = self.request.GET['latest']
    #thread_id =self.request.GET['thread_id']
    
    #print type(last_scan)
    latest_date=unicode_to_datetime(latest)
    
    req =Post_Update_Request(url=url,latest=latest_date)
    #print dir(self.request)
    req.put()
    e=error_tianya.OK("request accepted!%s"%self.request)
    self.render_error(e)
    

def main():
    import global_setting
    
    application = webapp.WSGIApplication(
                                       [('/apply_thread_update', MainPage)],
                                       debug=global_setting.is_debug())                                       
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
    main()

